<?php

/**
 * This is the model class for table "chatgroup".
 *
 * The followings are the available columns in table 'chatgroup':
 * @property string $id
 * @property string $group_id
 * @property string $topic
 * @property string $people
 * @property string $online
 * @property integer $create_time
 * @property integer $over_time
 * @property string $status
 */
class Chatgroup extends CActiveRecord {

    const STATUS_OK = 1;
    const STATUS_INVALID = 2;
    /**
     * Returns the static model of the specified AR class.
     * @return chatgroup the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'chatgroup';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('group_id', 'required'),
            array('group_id, topic', 'length', 'max' => 128),
            array('people, online', 'length', 'max' => 10),
            array('status', 'length', 'max' => 4),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, group_id, topic, people, online, create_time, over_time, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Id',
            'group_id' => 'Group',
            'topic' => 'Topic',
            'people' => 'People',
            'online' => 'Online',
            'create_time' => 'Create Time',
            'over_time' => 'Over Time',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);

        $criteria->compare('group_id', $this->group_id, true);

        $criteria->compare('topic', $this->topic, true);

        $criteria->compare('people', $this->people, true);

        $criteria->compare('online', $this->online, true);

        $criteria->compare('create_time', $this->create_time);

        $criteria->compare('over_time', $this->over_time);

        $criteria->compare('status', $this->status, true);

        return new CActiveDataProvider('chatgroup', array(
                    'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {
        $this->create_time = time();
        $this->status = self::STATUS_OK;
        $this->online = 1;
        $this->people = 1;
        return parent::beforeSave();
    }

}